#ifndef __can_Cfg_h__
#define __can_Cfg_h__

/**********************************************************************************
 *@file    : mid_Cfg.h 
 *@brief   : can 配置 head file
 *@author  : mcu
 *@version : Ver 1.1
 *@data    : 2018-03-23
 *@note    : Copyright(c) 2018 JJI. All rights reserved.
 ***********************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "types.h"
//#include "flexcan_driver.h"
/* Private define ------------------------------------------------------------*/
#include "CanTypes.h"
//CAN通道号
#define CAN0CH 0
#define CAN1CH 1
#define CAN2CH 2

#define    CAN_STD_ID    0
#define    CAN_EXT_ID    1


//-----------------------------end Can register -----------------------------------------
typedef struct {
    uint32_t cs;                        /*!< Code and Status*/
    uint32_t msgId;                     /*!< Message Buffer ID*/
    uint8_t data[64];                   /*!< Data bytes of the FlexCAN message*/
    uint8_t dataLen;                    /*!< Length of data in bytes */
} flexcan_msgbuff_t;

typedef struct {
    bool isRemoteFrame;      /*!< Remote frame*/
    bool isExtendedFrame;    /*!< Extended frame*/
    uint32_t *idFilter;      /*!< Rx FIFO ID filter elements*/
} flexcan_id_table_t;
/* Private typedef -----------------------------------------------------------*/
typedef void (* can_callback_t)(uint8_t instance, flexcan_msgbuff_t *msg);

//#define	CAN0_EN_OUTPUT		(PCC->PCCn[PCC_PORTA_INDEX ]|= PCC_PCCn_CGC_MASK,\
//								PORTA->PCR[6] |= PORT_PCR_MUX(1),\
//								PTA->PDDR  |=  (1<<6))
//#define	CAN0_EN_DIS			(PTA->PDOR &= ~(1<<6))
//#define	CAN0_EN_EN          (PTA->PDOR |= (1<<6))

//#define	CAN0_STB_OUTPUT			(PCC->PCCn[PCC_PORTE_INDEX ]|= PCC_PCCn_CGC_MASK,\
//								PORTE->PCR[11] |= PORT_PCR_MUX(1),\
//								PTE->PDDR  |=  (1<<11))
//#define	CAN0_STB_EN			    (PTE->PDOR |= (1<<11))
//#define	CAN0_STB_DIS			(PTE->PDOR &= ~(1<<11))

//#define	CAN0_WAKE_OUTPUT		(PCC->PCCn[PCC_PORTA_INDEX ]|= PCC_PCCn_CGC_MASK,\
//								PORTA->PCR[11] |= PORT_PCR_MUX(1),\
//								PTA->PDDR  |=  (1<<11))
//#define	CAN0_WAKE_DIS			(PTA->PDOR &= ~(1<<11))
//#define	CAN0_WAKE_EN			(PTA->PDOR |= (1<<11))

//#define	CAN0_FAULT_INPUT		(PCC->PCCn[PCC_PORTA_INDEX ]|= PCC_PCCn_CGC_MASK,\
//								PORTA->PCR[10] |= PORT_PCR_MUX(1),\
//								PTA->PDDR  &= ~(1<<10))
//#define	CAN0_FAULT_IS_HIGH      ((PTA->PDIR & 0x00000200) == 1)



typedef struct tag_struct_canMsg  /*  CAN message structure   */
{
	uint8_t  Code; 					/* Received message buffer code */
	uint8_t  Dlc;            		/* Recieved message number of data bytes */
	uint8_t  IdType;
	uint32_t  Id;                		/* Received message ID */
	uint8_t   Data[8];           	    /* Received message data string*/
	uint32_t  Timestamp;         		/* Received message time */
} struct_canMsg_t;

typedef enum
{
	CAN_OPERATING_UNINIT = 0,
	CAN_OPERATING_WORKING,
    CAN_OPERATING_SLEEP
} Can_workState_t;

typedef enum
{
	CAN_ERROR,  // error
	CAN_NORMAL,	// normal
	CAN_BUSOFF
}CAN_STATUS;

typedef struct  
{
    uint8_t detectTime;    
    uint8_t swCtrlStep; 
	CAN_STATUS status;
	uint8_t occurCnt;
	uint8_t occurFlag;  //occurCnt > 5
	uint8_t DtcStatus;
	uint32_t recoverTimeCnt;
	uint32_t timeoutCnt;
}struct_CanBusoff_t;

typedef struct CANState{
	Can_workState_t				workState;
	struct_CanBusoff_t			busOffState;
	uint8_t						CommunicationEnabled;
	
	can_callback_t				rxCallBack;
	struct_canMsg_t				*rxBuff;
	uint16_t					rxBuffSize;
	volatile uint16_t			rxInPtr;
	uint16_t					rxOutPtr;
	
	can_callback_t				txCallBack;
	struct_canMsg_t				*txBuff;
	uint16_t					txBuffSize;
	uint16_t					txInPtr;
	uint16_t					txOutPtr;
	boolean                     isTxBusy;
	uint32_t                    txPendingTimeLeft;

	can_callback_t				wakeUpCallBack;
	void (*busOffCallBack)(uint8_t instance);

	uint8_t						appPdu[8];
	uint32_t					appPduCycleMessageTimeLeft;
	int 						timehold;
} can_state_t;

/*---------------------------------------------------------------------------------------------------------*/

typedef struct {

	can_callback_t					rxCallBack;
	struct_canMsg_t					*rxBuff;
	uint16_t						rxBuffSize;

	can_callback_t					txCallBack;
	struct_canMsg_t					*txBuff;
	uint16_t						txBuffSize;

	can_callback_t					wakeUpCallBack;
	void (*busOffCallBack)(void);
} can_user_config_t;

#define TX_CANID_STD_MASK 0x000007FF
#define TX_CANID_EXT_MASK 0x1FFFFFFF

/* ================================================================================== */
/* =========================== CAN0 message start============================================ */
//send
#define DIAG_RESP		   0x7D2
#define DMS_1              0x40F


//receive
#define DIAG_PHYS		    0x752
#define DIAG_FUNC		    0x7DF
#define TIHU_IHU_11	        0x45A
#define ABS_ESP_G	        0x2C0
#define BCM_1	            0x5F0
#define SAM_1_G	            0x39C

extern uint32_t sg_can0FilterMaskIdList[];
extern const can_user_config_t can0_InitConfig;


/* Private functions ---------------------------------------------------------*/



#endif



